package com.jsh.erp.utils;


import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import okhttp3.*;

import java.io.IOException;
import java.util.Date;

@Slf4j
public class GdsUtils {
    public static String getGds(String gtin) {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .get()
                .url("https://bff.gds.org.cn/gds/searching-api/ProductService/ProductListByGTIN?PageSize=30&PageIndex=1&SearchItem="+ gtin)
                .addHeader("Accept", "application/json, text/plain, */*")
                .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
                .addHeader("Connection", "keep-alive")
                .addHeader("Origin", "https://www.gds.org.cn")
                .addHeader("Referer", "https://www.gds.org.cn/")
                .addHeader("Sec-Fetch-Dest", "empty")
                .addHeader("Sec-Fetch-Mode", "cors")
                .addHeader("Sec-Fetch-Site", "same-site")
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36")
                .addHeader("sec-ch-ua", "\"Google Chrome\";v=\"107\", \"Chromium\";v=\"107\", \"Not=A?Brand\";v=\"24\"")
                .addHeader("sec-ch-ua-mobile", "?0")
                .addHeader("sec-ch-ua-platform", "\"Windows\"")
                //.addHeader("Cookie", "pingtai5090=37132023")
                .build();
        try {
            Response response = client.newCall(request).execute();
            var result = response.body().string();
            log.info("gds gtin={}, result={}", gtin,result);
           return result;
        } catch (IOException e) {
            log.warn("gds", e);
            return null;
        }
    }


    public static void check(){
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .get()
                .url("https://bff.gds.org.cn/gds/searching-api/ProductService/ProductSimpleInfoByGTIN?gtin=06923807801639&id=EfDJ8L981XaqMyhOvdV8VZ1PDis75bQQu5Vs1MENCFSF7fq6pNdmMdOoHSRj9ekY84KuGDDTJo92VAK60SwBuOSdSB7BKdCjFYcdi8sIQ8nxCcwbDIIjviyoCGeVT4js0sPx6h4PQjzJz1kewmZegFzJyIyoS8IatveM08blYgKzaVmQ0BoYrziCpKX-8AHxehl1TxB410mbz_xcrXox5Fvmw7C")
                .addHeader("Accept", "application/json, text/plain, */*")
                .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
                .addHeader("Connection", "keep-alive")
                .addHeader("Origin", "https://www.gds.org.cn")
                .addHeader("Referer", "https://www.gds.org.cn/")
                .addHeader("Sec-Fetch-Dest", "empty")
                .addHeader("Sec-Fetch-Mode", "cors")
                .addHeader("Sec-Fetch-Site", "same-site")
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36")
                .addHeader("sec-ch-ua", "\"Google Chrome\";v=\"107\", \"Chromium\";v=\"107\", \"Not=A?Brand\";v=\"24\"")
                .addHeader("sec-ch-ua-mobile", "?0")
                .addHeader("sec-ch-ua-platform", "\"Windows\"")
                .addHeader("Cookie", "pingtai5090=37132023")
                .build();
        try {
            Response response = client.newCall(request).execute();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static Item get(String str){
        var entity = JSONObject.parseObject(str);
        if (null == entity || entity.getJSONObject("Data").getInteger("TotalCount") <= 0) {
            return null;
        }
        return entity.getJSONObject("Data").getJSONArray("Items").getJSONObject(0).toJavaObject(Item.class);
    }

    @Data
    public static class Item {
        private String keyword;
        @JsonProperty("branch_code")
        private String branchCode;
        private String gtin;
        @JsonProperty("f_id")
        private String fId;
        private String specification;
        private String brandid;
        @JsonProperty("base_id")
        private String baseId;
        @JsonProperty("is_private")
        private boolean isPrivate;
        @JsonProperty("firm_name")
        private String firmName;
        private String brandcn;
        @JsonProperty("picture_filename")
        private String pictureFilename;
        private String description;
        @JsonProperty("logout_flag")
        private String logoutFlag;
        @JsonProperty("have_ms_product")
        private int haveMsProduct;
        @JsonProperty("base_create_time")
        private Date baseCreateTime;
        @JsonProperty("branch_name")
        private String branchName;
        @JsonProperty("base_source")
        private String baseSource;
        private String gpc;
        private String gpcname;
        private Date saledate;
        private int saledateyear;
        @JsonProperty("base_last_updated")
        private Date baseLastUpdated;
        @JsonProperty("base_user_id")
        private String baseUserId;
        private String code;
        private String levels;
        @JsonProperty("levels_source")
        private String levelsSource;
        @JsonProperty("valid_date")
        private Date validDate;
        @JsonProperty("logout_date")
        private String logoutDate;
        private int gtinstatus;
    }

    /*{
        "Code": 1,
            "Msg": "Success",
            "Data": {
        "PageIndex": 1,
                "PageSize": 30,
                "TotalCount": 1,
                "TotalPages": 1,
                "HasPreviousPage": false,
                "HasNextPage": false,
                "Items": [
        {
            "keyword": "",
            "branch_code": "4402    ",
            "gtin": "06944857502390",
            "f_id": "ofDJ8L981XaqMyhOvdV8VZ1PDitQod5h3ueQC69qK0coDOXnYwjszQb-kYSAsKB883zR0Rvlcmgr32euSmdigSBS6Cx3MvF8_4YveAcp_KLRS02VcLz8mjDK5GlXImcNcDyFwPcogByIdHyNMQm18krC1Y5B3R2Psn9U10_5CXhjtzBShFZpLOpyy1QgQGbuxTRSrHQBq91T9mEl--pgw3VWR6C",
            "specification": "45g",
            "brandid": "QfDJ8L981XaqMyhOvdV8VZ1PDiu0xDVvMqNNYM3SuVRZxHwahAMX8dGHPK2L0o6JsAnXp9x3mq3fqwGbHCNn7QEh1mmryW_DfrB9YPmrJg3vJnXysLapQ_XgigEaLV_tkwufAtieDQUJVwEpqhF-u6ekONLCo88DNScrMyQ8xAPnwLs3sKZojJMwmHR1EXX2hjFbA-vbH2nJilW9cm3WqmQPXgC",
            "base_id": "0fDJ8L981XaqMyhOvdV8VZ1PDiuSAqQS4mMGExS8XbJ3jyiX4IXHoygoqxMR7sgFlrzo3K_dKs3obJA0FPK3SnllaHjQSkRM8agontyw0cz8kBWw_GLSmvNk51IRTvdzQb9hSLKHY6Y5pb1_yPfM0EsBdVjQbIwt6ooxs8ZjT4YU9bQR2Bm9Ly_fmoNROdAfSeAB1XwrpZBI_rPuOR5Eda5SU2C",
            "is_private": false,
            "firm_name": "深圳市塔妮娜美容健康护理用品有限公司",
            "brandcn": "歆宝",
            "picture_filename": null,
            "description": "婴儿桃子爽身啫喱",
            "logout_flag": "0",
            "have_ms_product": 0,
            "base_create_time": "2012-12-11T06:49:24.843Z",
            "branch_name": "深圳分中心",
            "base_source": "WWW",
            "gpc": "10000356",
            "gpcname": "护肤/保湿产品",
            "saledate": "2018-04-04T02:42:00Z",
            "saledateyear": 2018,
            "base_last_updated": "2021-12-30T06:32:10.02Z",
            "base_user_id": "1079885",
            "code": "69448575",
            "levels": null,
            "levels_source": null,
            "valid_date": "2023-12-25T16:00:00Z",
            "logout_date": null,
            "gtinstatus": 1
        }
        ]
    }
    }*/
}
